package devices

import (
	"errors"
	"fmt"
	"os"
)

const (
	GPIO          = "/sys/class/gpio"
	GPIO_EXPORT   = GPIO + "/export"
	GPIO_UNEXPORT = GPIO + "/unexport"
	// GPIO_FMT_DIRECTION = GPIO + "/direction"
)

var (
	// GPIO_ERROR_NOT_EXPORTED
	ErrGPIONotExported       error = errors.New("GPIO Not Exported")
	ErrGPIODirectionNotValue error = errors.New("GPIO Direction Not Value")
)

// GPIO Direction Mode
type GPIOMode int

const (
	DIRECTION_IN GPIOMode = iota
	DIRECTION_OUT
)

// GPIO Level Value
type GPIOValue int

const (
	LEVEL_LOW GPIOValue = iota
	LEVEL_HIGH
)

// GPIO Edge
type GPIOEdge string

const (
	EDGE_NONE    GPIOEdge = "none"
	EDGE_RISING  GPIOEdge = "rising"
	EDGE_FALLING GPIOEdge = "falling"
	EDGE_BOTH    GPIOEdge = "both"
)

var EDGE_VALUES []GPIOEdge

func init() {
	EDGE_VALUES = []GPIOEdge{
		EDGE_NONE,
		EDGE_RISING,
		EDGE_FALLING,
		EDGE_BOTH,
	}
}

func GPIOExport(gpio int) error {
	buffer := fmt.Sprintf("%d", gpio)
	return os.WriteFile(GPIO_EXPORT, []byte(buffer), os.ModePerm)
}

func GPIOUnExport(gpio int) error {
	buffer := fmt.Sprintf("%d", gpio)
	return os.WriteFile(GPIO_UNEXPORT, []byte(buffer), os.ModePerm)
}
